//: C20:TokenIteratorTest.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 1999
// Copyright notice in Copyright.txt
#include "TokenIterator.h"
#include "../require.h"
#include <fstream>
#include <iostream>
#include <vector>
#include <deque>
#include <set>
using namespace std;

int main()
{
    ifstream in("TokenIteratorTest.cpp");
    assure(in, "TokenIteratorTest.cpp");
    ostream_iterator<string> out(cout, "\n");
    typedef istreambuf_iterator<char> IsbIt;
    IsbIt begin(in), isbEnd;
    Delimiters
    delimiters(" \t\n~;()\"<>:{}[]+-=&*#.,/\\");
    TokenIterator<IsbIt, Delimiters>
    wordIter(begin, isbEnd, delimiters),
    end;
    vector<string> wordlist;
    copy(wordIter, end, back_inserter(wordlist));
    // Output results:
    copy(wordlist.begin(), wordlist.end(), out);
    *out++ = "-----------------------------------";
    // Use a char array as the source:
    char* cp =
        "typedef std::istreambuf_iterator<char> It";
    TokenIterator<char*, Delimiters>
    charIter(cp, cp + strlen(cp), delimiters),
    end2;
    vector<string> wordlist2;
    copy(charIter, end2, back_inserter(wordlist2));
    copy(wordlist2.begin(), wordlist2.end(), out);
    *out++ = "-----------------------------------";
    // Use a deque<char> as the source:
    ifstream in2("TokenIteratorTest.cpp");
    deque<char> dc;
    copy(IsbIt(in2), IsbIt(), back_inserter(dc));
    TokenIterator<deque<char>::iterator,Delimiters>
    dcIter(dc.begin(), dc.end(), delimiters),
    end3;
    vector<string> wordlist3;
    copy(dcIter, end3, back_inserter(wordlist3));
    copy(wordlist3.begin(), wordlist3.end(), out);
    *out++ = "-----------------------------------";
    // Reproduce the Wordlist.cpp example:
    ifstream in3("TokenIteratorTest.cpp");
    TokenIterator<IsbIt, Delimiters>
    wordIter2(IsbIt(in3), isbEnd, delimiters);
    set<string> wordlist4;
    while (wordIter2 != end)
        wordlist4.insert(*wordIter2++);
    copy(wordlist4.begin(), wordlist4.end(), out);
} ///:~
